import 'package:bluetooth_protocols_parser_plugin/library.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:inmotion_pro/pages/models/base_model.dart';
import 'package:safemap/safemap.dart';

import '../others/common_enum/firmware_update_type.dart';

// 固件信息模型
class FirmwareInfoModel extends BaseModel {
  // 设备号
  int deviceId = 0;

  // 应用程序版本号
  String appVersion = "";

  // 引导程序版本号
  String bootVersion = '';

  // 硬件版本号
  String hwVersion = '';

  // 设备类型名称
  String deviceTypeName = "";

  // 设备类型
  TargetDeviceType? get deviceType =>
      TargetDeviceType.getTypeByNum(deviceId, 0);

  // 升级文件路径
  String file = "";

  // 最新版本号
  RxString latestVersion = "".obs;

  // 升级状态
  Rx<UpgradeStatus> status = Rx<UpgradeStatus>(UpgradeStatus.upToDate);

  // 升级进度
  RxInt progress = 0.obs;

  // 升级进度描述
  RxString progressDescription = "".obs;

  // 更新说明
  RxString updateDescription = "".obs;

  FirmwareInfoModel.fromMap(Map<String, dynamic> map, {bool isLR1 = false}) {
    SafeMap safeMap = SafeMap(map);
    deviceId = safeMap['device_id'].toInt ?? safeMap['device'].toInt ?? 0;
    appVersion =
        safeMap['app_version'].string ?? safeMap['appVersion'].string ?? '';
    bootVersion =
        safeMap['boot_version'].string ?? safeMap['blVersion'].string ?? '';
    hwVersion =
        safeMap['hw_version'].string ?? safeMap['hwVersion'].string ?? '';
    deviceTypeName =
        TargetDeviceType.getTypeByNum(deviceId, isLR1 ? 1 : 0)?.name ?? "";
  }

  @override
  Map<String, dynamic> toMap() {
    return {
      'device_id': deviceId,
      'app_version': appVersion,
      'boot_version': bootVersion,
      'hw_version': hwVersion,
      'device_type_name': deviceTypeName,
    };
  }

  // 通过 type 获取统称名称
  String get deviceTypeGeneralName {
    switch (deviceType) {
      case TargetDeviceType.eucHmic:
        return local.human_board;
      case TargetDeviceType.eucDriver:
        return local.driver_board;
      case TargetDeviceType.eucBle:
        return local.bluetooth_board;
      case TargetDeviceType.eucBMS1:
        return local.BMS_1;
      case TargetDeviceType.eucBMS2:
        return local.BMS_2;
      case TargetDeviceType.eucDc:
        return local.display;
      case TargetDeviceType.eucTBox:
        return local.tbox_set;
      case TargetDeviceType.scooterDriver:
        return local.driver_board;
      case TargetDeviceType.scooterDisplay:
        return local.display;
      default:
        return local.unknown_device;
    }
  }
}
